define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: () => {
            const { reactive, onMounted } = Vue
            const { ElMessageBox } = ElementPlus
            const index = {
                setup() {
                    const state = reactive({
                        data: [],
                        order: '',
                        sort: '',
                        filter: {
                            drawer: false,
                            data: {
                                status: 'all',
                                store_name: '',
                                createtime: [],
                                updatetime: [],
                            },
                            tools: {
                                store_name: {
                                    type: 'tinput',
                                    label: '商户名称',
                                    placeholder: '请输入商户名称',
                                    value: '',
                                },
                                createtime: {
                                    type: 'tdatetimerange',
                                    label: '申请时间',
                                    value: [],
                                },
                                updatetime: {
                                    type: 'tdatetimerange',
                                    label: '上次操作时间',
                                    value: [],
                                },
                            },
                            condition: {},
                        },
                        statusStyle: {
                            '-1': 'sa-color--danger',
                            0: 'sa-color--info',
                            1: 'sa-color--success',
                        }
                    })

                    const type = reactive({
                        data: {
                            status: [{
                                name: '全部',
                                type: 'all',
                            },
                            {
                                name: '待审核',
                                type: '0',
                            },
                            {
                                name: '已同意',
                                type: '1',
                            },
                            {
                                name: '已拒绝',
                                type: '-1',
                            },],
                        }
                    })

                    function getData() {
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
                        let search = composeFilter(tempSearch, {
                            store_name: 'like',
                            createtime: 'range',
                            updatetime: 'range',
                        });
                        Fast.api.ajax({
                            url: 'sum/store/store',
                            type: 'GET',
                            data: {
                                page: pagination.page,
                                list_rows: pagination.list_rows,
                                order: state.order,
                                sort: state.sort,
                                ...search,
                            },
                        }, function (ret, res) {
                            state.data = res.data.data
                            pagination.total = res.data.total
                            return false
                        }, function (ret, res) { })
                    }

                    function onChangeSort({ prop, order }) {
                        state.order = order == 'ascending' ? 'asc' : 'desc';
                        state.sort = prop;
                        getData();
                    }
                    function onOpenFilter() {
                        state.filter.drawer = true
                    }
                    function onChangeFilter() {
                        pagination.page = 1
                        getData()
                        state.filter.drawer && (state.filter.drawer = false)
                    }

                    function onChangeTab() {
                        pagination.page = 1
                        getData()
                    }

                    const pagination = reactive({
                        page: 1,
                        list_rows: 10,
                        total: 0,
                    })

                    const batchHandle = reactive({
                        data: [],
                    })
                    function onChangeSelection(val) {
                        batchHandle.data = val
                    }
                    function onBatchHandle(type) {
                        let ids = []
                        batchHandle.data.forEach((item) => {
                            ids.push(item.id)
                        })
                        switch (type) {
                            case 'delete':
                                ElMessageBox.confirm('此操作将删除, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning',
                                }).then(() => {
                                    onDelete(ids.join(','))
                                });
                                break;
                            default:
                                Fast.api.ajax({
                                    url: `sum/store/store/edit/id/${ids.join(',')}`,
                                    type: 'POST',
                                    data: {
                                        status: type
                                    }
                                }, function (ret, res) {
                                    getData()
                                }, function (ret, res) { })
                                break;
                        }
                    }

                    const handlePopover = reactive({
                        flag: {}
                    })

                    function onAdd() {
                        Fast.api.open("sum/store/store/add?type=add", "添加", {
                            callback() {
                                getData()
                            }
                        })
                    }
                    function onEdit(id) {
                        Fast.api.open(`sum/store/store/edit?type=edit&id=${id}`, "编辑", {
                            callback() {
                                getData()
                            }
                        })
                    }
                    function onDelete(id) {
                        Fast.api.ajax({
                            url: `sum/store/store/delete/id/${id}`,
                            type: 'DELETE',
                        }, function (ret, res) {
                            getData()
                        }, function (ret, res) { })
                    }
                    function onRecyclebin() {
                        Fast.api.open('sum/store/store/recyclebin', "回收站", {
                            callback() {
                                getData()
                            }
                        })
                    }

                    function onUser(key,title) {
                        Fast.api.addtabs(`sum/store/user/index?store_id=${key}&store_name=${title}`, title+'-员工管理')
                    }
                    function onGoods(key,title) {
                        Fast.api.addtabs(`sum/store/goods/index?store_id=${key}&store_name=${title}`, title+'-商品')
                    }
                    function onOrder(key,title) {
                        Fast.api.addtabs(`sum/store/order/index?store_id=${key}&store_name=${title}`, title+'-店铺订单')
                    }
                    function onStockOrder(key,title) {
                        Fast.api.addtabs(`sum/store/stock_order/index?store_id=${key}&store_name=${title}`, title+'-进货订单')
                    }
                    function onActivity(key,title) {
                        Fast.api.addtabs(`sum/store/activity/activity/index?type=groupon&store_id=${key}&store_name=${title}`, title+'-拼团活动')
                    }


                    onMounted(() => {
                        getData()
                    })

                    return {
                        onClipboard,
                        state,
                        type,
                        getData,
                        onChangeSort,
                        onOpenFilter,
                        onChangeFilter,
                        onChangeTab,
                        pagination,
                        handlePopover,
                        batchHandle,
                        onChangeSelection,
                        onBatchHandle,
                        onAdd,
                        onEdit,
                        onDelete,
                        onRecyclebin,
                        onGoods,
                        onOrder,
                        onUser,
                        onStockOrder,
                        onActivity,
                    }
                }
            }
            createApp('index', index);
        },
        add: () => {
            Controller.form();
        },
        edit: () => {
            Controller.form();
        },
        form: () => {
            const { reactive, onMounted, getCurrentInstance } = Vue
            const addEdit = {
                setup() {
                    const { proxy } = getCurrentInstance();

                    const state = reactive({
                        type: new URLSearchParams(location.search).get('type'),
                        id: new URLSearchParams(location.search).get('id')
                    })

                    const form = reactive({
                        model: {
                            image: '',
                            store_name: '',
                            user_name: '',
                            phone: '',
                            dian:[],
                            status: 'normal',
                            province_name: '',
                            province_id: '',
                            city_name: '',
                            city_id: '',
                            district_name: '',
                            district_id: '',
                            pcd:[],
                        },
                        rules: {
                            image: [{ required: true, message: '请选择logo图片', trigger: 'blur' }],
                            store_name: [{ required: true, message: '请输入商户名称', trigger: 'blur' }],
                            user_name: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
                            phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
                            pcd: [{ required: true, message: '请选择省市', trigger: 'blur' }],
                        },
                    })

                    function getDetail() {
                        Fast.api.ajax({
                            url: `sum/store/store/detail/id/${state.id}`,
                            type: 'GET',
                        }, function (ret, res) {
                            form.model = res.data;
                            form.model.pcd=[
                                res.data.province_id,
                                res.data.city_id,
                                res.data.district_id,
                            ];
                            return false
                        }, function (ret, res) { })
                    }

                    function onConfirm() {

                        proxy.$refs['formRef'].validate((valid) => {
                            if (valid) {
                                let label = proxy.$refs['addressRef'].getCheckedNodes()[0].pathLabels;
                                let tempForm = JSON.parse(JSON.stringify(form.model));
                                tempForm = {
                                    ...tempForm,
                                    province_name: label[0],
                                    province_id: form.model.pcd[0],
                                    city_name: label[1],
                                    city_id: form.model.pcd[1],
                                    district_name: label[2],
                                    district_id: form.model.pcd[2],
                                };
                                delete tempForm.pcd;
                                Fast.api.ajax({
                                    url: state.type == 'add' ? 'sum/store/store/add' : `sum/store/store/edit/id/${state.id}`,
                                    type: 'POST',
                                    data: tempForm,
                                }, function (ret, res) {
                                    Fast.api.close()
                                }, function (ret, res) { })
                            }
                        });
                    }

                    function onAddDian() {
                        form.model.dian.push({
                            dian_address: '',
                            dian_name: '',
                        });
                    }
                    function onDeleteDian(index) {
                        form.model.dian.splice(index, 1);
                    }

                    const area = reactive({
                        select: []
                    })
                    function getAreaSelect() {
                        Fast.api.ajax({
                            url: 'sum/data/area/select',
                            type: 'GET',
                        }, function (ret, res) {
                            area.select = res.data;
                            return false
                        }, function (ret, res) { })
                    }

                    onMounted(() => {
                        state.type == 'edit' && getDetail()
                        getAreaSelect()
                    })

                    return {
                        state,
                        form,
                        onAddDian,
                        onDeleteDian,
                        area,
                        getAreaSelect,
                        onConfirm
                    }
                }
            }
            createApp('addEdit', addEdit);
        },
        recyclebin: () => {
            const { reactive, onMounted } = Vue
            const { ElMessageBox } = ElementPlus
            const recyclebin = {
                setup() {
                    const state = reactive({
                        data: [],
                        order: '',
                        sort: '',
                    })

                    function getData() {
                        Fast.api.ajax({
                            url: 'sum/store/store/recyclebin',
                            type: 'GET',
                            data: {
                                page: pagination.page,
                                list_rows: pagination.list_rows,
                                order: state.order,
                                sort: state.sort,
                            },
                        }, function (ret, res) {
                            state.data = res.data.data
                            pagination.total = res.data.total
                            return false
                        }, function (ret, res) { })
                    }

                    function onChangeSort({ prop, order }) {
                        state.order = order == 'ascending' ? 'asc' : 'desc';
                        state.sort = prop;
                        getData();
                    }

                    const pagination = reactive({
                        page: 1,
                        list_rows: 10,
                        total: 0,
                    })

                    const batchHandle = reactive({
                        data: [],
                        tools: [
                            {
                                type: 'restore',
                                label: '还原',
                                class: 'primary',
                            },
                            {
                                type: 'destroy',
                                label: '销毁',
                                class: 'danger',
                            },
                            {
                                type: 'all',
                                label: '清空回收站',
                                class: 'danger',
                            },
                        ]
                    })
                    function onChangeSelection(val) {
                        batchHandle.data = val
                    }
                    function onBatchHandle(type) {
                        let ids = []
                        batchHandle.data.forEach((item) => {
                            ids.push(item.id)
                        })
                        switch (type) {
                            case 'restore':
                                onRestore(ids.join(','))
                                break;
                            case 'destroy':
                                ElMessageBox.confirm('此操作将销毁, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning',
                                }).then(() => {
                                    onDestroy(ids.join(','))
                                });
                                break;
                            case 'all':
                                ElMessageBox.confirm('此操作将清空回收站, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning',
                                }).then(() => {
                                    onDestroy('all')
                                });
                                break;
                        }
                    }

                    function onRestore(id) {
                        Fast.api.ajax({
                            url: `sum/store/store/restore/id/${id}`,
                            type: 'POST',
                        }, function (ret, res) {
                            getData()
                        }, function (ret, res) { })
                    }
                    function onDestroy(id) {
                        Fast.api.ajax({
                            url: `sum/store/store/destroy/id/${id}`,
                            type: 'POST',
                        }, function (ret, res) {
                            getData()
                        }, function (ret, res) { })
                    }

                    onMounted(() => {
                        getData()
                    })

                    return {
                        state,
                        getData,
                        onChangeSort,
                        pagination,
                        batchHandle,
                        onChangeSelection,
                        onBatchHandle,
                        onRestore,
                        onDestroy,
                    }
                }
            }
            createApp('recyclebin', recyclebin);
        },
    };
    return Controller;
});
